1. Field of the Invention
The present invention relates generally to memory usage, and more particularly, to methods and systems for efficiently allocating memory.
2. Description of the Related Art
Computer memory systems are used to store objects that are being used by one or more applications running on the computer. Typically, most of these objects (e.g. 75-95%) have a relatively short period of time in which they are used. Some of these objects (e.g., 5-25%) have a relatively longer period of time in which they are used and therefore should ideally be maintained in the computer memory system so that these objects are readily available to the on-going applications. Computer memory systems are regularly and routinely purged of such unused objects to ensure the unused objects do not needlessly consume precious memory capacity. This purging process is typically referred to as a garbage collection scheme.
In a typical, generational, garbage collection scheme the memory heap is divided into three sections. A first section of the memory heap is allocated as a tenured section. An adjacent, second section is allocated as a young section and a third section of the memory heap is an unused space section. The tenured section includes memory objects that have survived at least one prior garbage collection. The tenured section is typically in a single contiguous block. The young section includes memory objects that have been allocated since the immediate prior garbage collection process. The young section is also typically in a contiguous block. The unused space section is the memory space that does not belong to either of the two section types above.
The young section is placed in the addresses that immediately follow the tenured section. By way of example, the tenured section may be in addresses 0 to 1000 and the young section is located in addresses 1001 to 3000. The unused space section falls into the remaining portion of the memory heap, subsequent to the young section.
Initially, applications fill the young section with objects. When the young section is filled with objects, a garbage collection scheme is applied to the young section. During each iteration of the garbage collection process, the objects in the current young section are examined and those objects that are no longer referenced are deleted. Those objects that are still referenced are shifted into the tenured section. Each iteration of the garbage collection enlarges the tenured section.
As the tenured section is enlarged, the young section is continuously shifted to higher memory addresses (i.e., those addresses immediately following the ever enlarged tenured section). When the tenured section becomes so large that the combination of the young section and the tenured section consume a preselected portion of or the entire memory heap (i.e., the unused space section approaches a predetermined minimum limit) then, the garbage collection is applied to both the young section and the tenured section. The garbage collection then deletes those objects that are no longer referenced in both the young section and the tenured section. All of the remaining objects are shifted downward to create a new tenured section. As a result, additional memory space is freed up for subsequent operations.
By way of example, initially a 1 megabyte tenured section is allocated and an immediately adjacent 1 megabyte is allocated to young section. The applications begin to use the young section. When the young section is filled with objects, then the garbage collection scheme is applied to the young section and approximately 75-95% (e.g., 750-950 k) of objects are deleted from the young section. The remaining 50-250 k of objects are then shifted into the tenured section. In subsequent iterations of the garbage collection the tenured section will be filled. Once the tenured section is filled, the 50-250 k of objects that remain in the young section are shifted downward and appended to the tenured section to form the enlarged tenured section having a 1.05-1.25.megabyte size.
The above process continues iteratively until the size of the unused section becomes smaller than a set parameter (e.g., zero). A garbage collection scheme is then applied to the both the enlarged tenured section and the young section to eliminate unused objects from the enlarged tenured section and young section and thereby reduce the memory space consumed by the objects to less than the originally allocated tenured section size (e.g., 1 megabyte).
By way of example, typically the enlarged tenured section and the young section will be reduced by about 75-95% so that the tenured section can be reallocated to only 1 megabyte. The process can then begin again to fill the tenured section and then fill and garbage collect the young section as described above.
While a typical garbage collection scheme can manage the consumption of memory, the young section presents moving targets as the size of the tenured section varies. In view of the foregoing, there is a need for a system and a method for garbage collection where the respective locations of the young section and the tenured space are set and do not present a moving target and therefore can be more efficiently used.